using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using EntLibEC.Cmn;
using EntLibEC.Biz.Report;
using EntLibEC.Biz;

namespace EntLibEC.View.Report
{
	/// <summary>
	/// Summary description for SaleReportByPM.
	/// </summary>
	public partial class SaleReportByPM : PageBase
	{
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			base.CheckAccess((int)AppEnum.Privilege.SaleReportSearch,true ,null);
			txtDateFrom.Attributes["onclick"]= "javascript:calendar()";
			txtDateTo.Attributes["onclick"]= "javascript:calendar()";
			if ( !Page.IsPostBack)
			{
				txtDateFrom.Text = DateTime.Now.ToString(AppConst.DateFormat).Substring(0,7) + "-01";
				txtDateTo.Text = DateTime.Now.ToString(AppConst.DateFormat);
				btnExport.Enabled = false;
                ucProductType.BindProductType(true);
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		private bool checkForm()
		{
			if ( !checkDate(txtDateFrom, "Date From") || !checkDate(txtDateTo, "Date To"))
				return false;

			return true;

		}
		private bool checkDate(TextBox txt, string txtName)
		{
			if ( txt.Text.Trim() == "")
			{
				Assert(lblMessage, txtName + " can't be empty", -1);
				return false;
			}
			else
			{
				try
				{
					Convert.ToDateTime(txt.Text.Trim());
				}
				catch
				{
					Assert(lblMessage, txtName + " format error", -1);
					return false;
				}
			}
			return true;

		}

		protected void btnSearch_Click(object sender, System.EventArgs e)
		{
			if ( !checkForm())
				return;

			Hashtable ht = new Hashtable(10);
			ht.Add("DateFrom", txtDateFrom.Text.Trim());
			ht.Add("DateTo", txtDateTo.Text.Trim());

            if (ucProductType.ProductTypeSysNo != AppConst.IntNull)
                ht.Add("ProductType", ucProductType.ProductTypeSysNo);
			
			DataSet ds = ReportManager.GetInstance().GetSaleReportDsGroupByPM(ht);

			DataGrid1.DataSource = ConvertDs(ds);
			DataGrid1.DataBind();

			int rows = 0;
			if ( Util.HasMoreRow(ds) )
			{
				rows = ds.Tables[0].Rows.Count;
				btnExport.Enabled = true;
			}
			Assert(lblMessage, rows.ToString() + " record(s)", 1);
		
		}

		private DataSet ConvertDs(DataSet ds)
		{
			if ( !Util.HasMoreRow(ds))
				return null;

			ds.Tables[0].Columns.Add("TotalSaleCvt");
			ds.Tables[0].Columns.Add("TotalCostCvt");
			ds.Tables[0].Columns.Add("ProfitCvt");
			ds.Tables[0].Columns.Add("RateCvt");
			ds.Tables[0].Columns.Add("TotalStockCostCvt");
			
			foreach( DataRow dr in ds.Tables[0].Rows )
			{
				decimal totalCost = Util.TrimDecimalNull(dr["totalCost"]);
				decimal totalSale = Util.TrimDecimalNull(dr["totalSale"]);
				decimal totalStockCost = Util.TrimDecimalNull(dr["totalStockCost"]);

				dr["TotalCostCvt"] = (totalCost/1.17m).ToString(AppConst.DecimalFormatWithGroup);
				dr["TotalSaleCvt"] = (totalSale/1.17m).ToString(AppConst.DecimalFormatWithGroup);
				dr["TotalStockCostCvt"] = (totalStockCost/1.17m).ToString(AppConst.DecimalFormatWithGroup);

				decimal totalProfit = totalSale - totalCost;
				dr["ProfitCvt"] = (totalProfit/1.17m).ToString(AppConst.DecimalFormatWithGroup);

				if ( totalSale != 0)
					dr["RateCvt"] = ( (totalProfit/totalSale) * 100).ToString(AppConst.DecimalFormat) + "%";
				else
					dr["RateCvt"] = "N/A";
			}
			return ds;
		}

		protected void btnExport_Click(object sender, System.EventArgs e)
		{
			Util.ToExcel(DataGrid1, "Sale Report By PM", "");
		}
	}
}
